package it.ap.sa.core.utils;

import org.springframework.util.StopWatch;

/**
 * @author Alessandro Paolillo
 */
public class SmStopWatch extends StopWatch{

	public SmStopWatch() {
		super();
	}

	public SmStopWatch(String id) {
		super(id);
	}

	/**
	 * @author Alessandro Paolillo
	 */
	@Override
	public String shortSummary() {
		return "StopWatch '" + this.getId() + "': running time = " + this.formatExecutionTime();
	}
	/**
	 * @author Alessandro Paolillo
	 */
	private String formatExecutionTime(){
		long executionTime = this.getTotalTimeMillis();
		long milliseconds  = executionTime - ((long)(executionTime/1000)) * 1000;
		long seconds       = (executionTime - milliseconds)/1000;
		long minutes       = seconds/60;

		seconds = seconds - minutes * 60;

		String result = minutes
				      + ":" + (seconds < 10 ? "0" + seconds : seconds)
				      + "." + formatMilliSeconds(milliseconds)
				      ;
		return result;
	}
	/**
	 * @author Alessandro Paolillo
	 */
	private String formatMilliSeconds(long milliseconds){
		return  milliseconds < 10
			  ? "00" + milliseconds
			  : (milliseconds < 100
				 ? "0" + milliseconds
				: milliseconds + ""
				)
			  ;
	}



}
